package com.ruan.service;

import com.ruan.dao.UserMapper;
import com.ruan.openfeign.RoleCenterOpenfeign;
import com.ruan.pojo.Result;
import com.ruan.pojo.Role;
import com.ruan.pojo.User;
import com.ruan.query.UserQuery;
import com.ruan.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName:UserService
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/6/14 15:06
 **/
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private RoleCenterOpenfeign roleCenterOpenfeign;

    public UserVo getUserInfoById(Integer id) {

        //先拿到user
        User user=userMapper.getUserInfoById(id);
        //发生请求到role-center
        RestTemplate restTemplate=new RestTemplate();
        //post方式与get方式
        //访问对应controller，返回的时role类型，所以要在这个项目里创建role
        //根据user表中的roleid到role表中查询rolename，不能直接连表查询，因为不在一个数据库中
        Role role= roleCenterOpenfeign.getRoleById(user.getRoleId());

        UserVo userVo= new UserVo();
        //快捷方式，将user里的属性设置成uservo里的属性
        BeanUtils.copyProperties(user,userVo);
        userVo.setRoleName(role.getRoleName());

        return  userVo;

    }

    //用户分页模糊查询
    public List<UserVo> getUserList(UserQuery userQuery) {

        List<UserVo> userVoList = userMapper.getUserList(userQuery);

        for(UserVo i:userVoList){

            i.setRoleName(roleCenterOpenfeign.getRoleById(i.getRoleId()).getRoleName());
            //Role role = restTemplate.getForObject("http://role-center/role/getRoleById/{id}",Role.class,i.getRoleId());

        }

        return userVoList;

    }

    public Result addUser(User user) {

        User user1 = userMapper.getUserByUserName(user.getUserName());

        if(user1 != null){

            return new Result(100,"该用户已被注册");

        }

        int row = userMapper.addUser(user);

        if(row == 1){

            return new Result(200,"添加成功");

        }else {

            return new Result(100,"添加失败");

        }
    }

    public Result deleteUserById(Integer id) {

        int row = userMapper.deleteUserById(id);

        if(row == 1){

            return new Result(200,"删除成功");

        }else {

            return new Result(100,"删除失败");

        }

    }

    public Result editUser(User user) {

        int row = userMapper.editUser(user);

        if(row == 1){

            return new Result(200,"修改成功");

        }else {

            return new Result(100,"修改失败");

        }

    }

    public Result deleteUserByIds(Integer[] ids) {

        int row = userMapper.deleteUserByIds(ids);

        if(row == ids.length){

            return new Result(200,"删除成功");

        }else {

            return new Result(100,"删除失败");

        }
    }

    public User getUserById(Integer id) {

        return userMapper.getUserById(id);

    }
}
